package hoyo.zqy.org.service.impl;

import hoyo.zqy.basic.service.impl.BaseServiceImpl;
import hoyo.zqy.basic.util.Md5Utils;
import hoyo.zqy.basic.util.PageList;
import hoyo.zqy.basic.util.StrUtils;
import hoyo.zqy.org.domain.Employee;
import hoyo.zqy.org.mapper.EmployeeMapper;
import hoyo.zqy.org.query.EmployeeQuery;
import hoyo.zqy.org.service.IEmployeeService;
import hoyo.zqy.user.domain.Logininfo;
import hoyo.zqy.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    /**
     * 保存
     * @param employee
     */
    @Override
    public void save(Employee employee) {
        //处理颜值密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5 = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5);
        //同步到logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        logininfo.setDisable(true);
        //存入数据库，生成logininfo的ID
        logininfoMapper.save(logininfo);
        //添加LogininfoID
        employee.setLogininfo_id(logininfo.getId());
        super.save(employee);

        //为当前员工添加角色信息
        if (employee.getRole_id()!=null){
            employeeMapper.addRole(employee);
        }
    }

    /**
     * 删除
     * @param id
     */
    @Override
    @Transactional
    public void delete(Long id) {
        //删除角色信息
        employeeMapper.delEmployeeRoleByEid(id);
        //查询对应数据
        Employee employee = employeeMapper.loadById(id);
        //根据数据内LogininfoID进行删除
        logininfoMapper.delete(employee.getLogininfo_id());
        super.delete(id);
    }

    /**
     * 修改
     * @param employee
     */
    @Override
    public void update(Employee employee) {
        //处理颜值密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5 = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5);

        //查询原来的employee对象
        Employee oldemployee = employeeMapper.loadById(employee.getId());
        employee.setLogininfo_id(oldemployee.getLogininfo_id());
        employee.setShop_id(oldemployee.getShop_id());

        super.update(employee);
        //更新logininfo对象
        Logininfo oldlogininfo = logininfoMapper.loadById(oldemployee.getLogininfo_id());
        //属性同名原则，logininfo的id也会被修改
        BeanUtils.copyProperties(employee,oldlogininfo);
        //查询以前的employee的logininfoID进行修正
        oldlogininfo.setId(oldemployee.getLogininfo_id());
        logininfoMapper.update(oldlogininfo);
        //修改当前员工角色
        if (employee.getRole_id()!=null){
            //删除角色信息
            employeeMapper.delEmployeeRoleByEid(employee.getId());
            //再添加
            employeeMapper.addRole(employee);
        }
    }

    @Override
    public void deleBatch(Long[] ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    @Override
    public List<Employee> list(Long shopId) {
        return employeeMapper.loadByShopid(shopId);
    }
}
